package com.android;

import com.android.KickFlybug.Until.ActivityUtil;
import com.android.KickFlybug.Until.BitmapManager;
import com.android.KickFlybug.Until.GameObjData;
import com.android.KickFlybug.View.GameView;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class KickFly extends Activity
{
	//定义游戏的View
	GameView gv;
	//定义传感器
	private SensorManager sensorManager;
	//定义摇动消息
	private static final int SENSOR_SHAKE = 10; 
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//全屏并且没有title
		ActivityUtil.fullLandScapeScreen(this);
		ActivityUtil.initScreenData(this);
		//载入游戏资源文件，活虫子与死虫子的图片
		Resources res = getResources();
		BitmapManager.initFlyBitmap(res);
		//生成GameView的实例
		if (gv == null)
		{
			gv = new GameView(this);
		} else
		{
			Log.i(ActivityUtil.infoMessage, "GameView已存在");
		}
		setContentView(gv);
		//获取传感器
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
	}

	@Override
	protected void onResume() { 
	    super.onResume();
	    if (sensorManager != null) {// 注册监听器
	    	sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); 
	    		// 第一个参数是Listener，第二个参数是所得传感器类型，第三个参数值获取传感器信息的频率 
	    }
	 }
	@Override 
	protected void onStop() { 
	    super.onStop(); 
	    if (sensorManager != null) {// 取消监听器 
	        sensorManager.unregisterListener(sensorEventListener); 
	    } 
	} 
	 /**
     * 重力感应监听
     */ 
    private SensorEventListener sensorEventListener = new SensorEventListener() { 
 
        @Override 
        public void onSensorChanged(SensorEvent event) { 
            // 传感器信息改变时执行该方法 
            float[] values = event.values; 
            float x = values[0]; // x轴方向的重力加速度，向右为正 
            float y = values[1]; // y轴方向的重力加速度，向前为正 
            float z = values[2]; // z轴方向的重力加速度，向上为正 
            Log.i("Gravitational acceleration", "x轴" + x +  "；y轴" + y +  "；z轴" + z); 
            // 一般在这三个方向的重力加速度达到40就达到了摇晃手机的状态。 
            int medumValue = 19;// 三星 i9250怎么晃都不会超过20，没办法，只设置19了 
            if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) { 
            //    vibrator.vibrate(200);
                Message msg = new Message(); 
                msg.what = SENSOR_SHAKE; 
                handler.sendMessage(msg); 
            } 
        }

		@Override
		public void onAccuracyChanged(Sensor arg0, int arg1) {
			// TODO Auto-generated method stub
			
		} 
 
    }; 
 
    /**
     * 动作执行
     */ 
    Handler handler = new Handler() { 
 
        @Override 
        public void handleMessage(Message msg) { 
            super.handleMessage(msg); 
            switch (msg.what) { 
            case SENSOR_SHAKE: 
                Toast.makeText(KickFly.this, "清扫死蚊子！", Toast.LENGTH_SHORT).show(); 
                GameObjData.ISCLEAN = true;
                Log.i("Gravitational acceleration", "检测到摇晃，执行操作！"); 
                break; 
            } 
        } 
 
    }; 
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		Log.i(ActivityUtil.infoMessage, "Destroy GameView");
		GameObjData.clear();
		System.gc();
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		Log.i(ActivityUtil.infoMessage, "Pause GameView");
		gv.gameLoop = false;
	}

	@Override
	protected void onStart()
	{
		super.onStart();
		Log.i(ActivityUtil.infoMessage, "Start GameView");
		gv.initGameObjData();
	}
}